/*
 * Copyright 2007-2008 Brian Cavalier
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.bc.iterate;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * A TransformChain is a {@link org.bc.iterate.Transform} that wraps and applies a series of
 * {@link org.bc.iterate.Transform}s in sequence.
 * 
 * @author Brian Cavalier
 */
public class TransformChain<T> implements Transform<T>{
	private List<Transform<T>> transforms;
    
    public TransformChain(Transform<T>... t) {
        transforms = new ArrayList<Transform<T>>(t.length);
        Collections.addAll(transforms, t);
    }

    public T apply(T item) {
		T result = item;
		for(Transform<T> trans : getTransforms()) {
			result = trans.apply(result);
		}
		return result;
	}

    public TransformChain<T> then(Transform<T> nextTransform) {
        transforms.add(nextTransform);
        return this;
    }

    public List<Transform<T>> getTransforms()
    {
        return transforms;
    }

    public void setTransforms(List<Transform<T>> transforms)
    {
        //noinspection AssignmentToCollectionOrArrayFieldFromParameter
        this.transforms = new ArrayList<Transform<T>>(transforms);
    }
}